//package com.ruoyi.common.utils.fabric;
//
//import com.ruoyi.common.properties.FabricProperties;
//import org.hyperledger.fabric.sdk.*;
//import org.hyperledger.fabric.sdk.security.CryptoSuite;
//import org.hyperledger.fabric.sdk.security.CryptoSuiteFactory;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.PostConstruct;
//import java.io.ByteArrayOutputStream;
//import java.io.File;
//import java.io.FileNotFoundException;
//import java.io.InputStream;
//import java.nio.charset.StandardCharsets;
//import java.nio.file.Files;
//import java.nio.file.Path;
//import java.nio.file.StandardCopyOption;
//import java.util.Collection;
//
//import static io.ipfs.multibase.Base16.bytesToHex;
//
//@Service
//public class FabricClientService {
//
//    @Value("${fabric.networkConfigPath}")
//    private String connectionProfile;
//
//    private HFClient hfClient;
//    private Channel channel;
//
//    @Autowired
//    private FabricProperties fabricProperties;
//
//    private static FabricClientService instance;
//
//    // 私有构造函数，防止外部实例化
//    private FabricClientService() {
//        // 私有构造函数
//    }
//
//    // 提供一个全局访问点获取实例
//    public static synchronized FabricClientService getInstance() {
//        if (instance == null) {
//            instance = new FabricClientService();
//        }
//        return instance;
//    }
//
//    // 初始化HFClient
////    @PostConstruct
//    public void initHFClient() throws Exception {
//        if (hfClient == null) {
//            // 创建HFClient对象
//            hfClient = HFClient.createNewInstance();
//
//            // 创建并设置用户上下文
//            User user = new MyUser(fabricProperties.getUser(), fabricProperties.getCertificatePath(), fabricProperties.getPrivateKeyPath());
//            hfClient.setCryptoSuite(CryptoSuiteFactory.getDefault().getCryptoSuite());
//            hfClient.setUserContext(user);
//
//            // 创建Orderer实例
//            Orderer orderer = hfClient.newOrderer("orderer1.henriport.com", "grpcs://62.234.46.212:7004");
//            System.out.println("Orderer created: " + orderer);
//        }
//    }
//
//    public HFClient getHFClient() {
//        return hfClient;
//    }
//
//    public void createChannel(String channelName) throws Exception {
//        // Create a new channel if necessary
//        // 获取资源文件的输入流
//        InputStream inputStream = FabricClientService.class.getClassLoader().getResourceAsStream("mychannel.tx");
//        if (inputStream == null) {
//            throw new FileNotFoundException("mychannel.tx file not found in resources");
//        }
//
//        // 使用 ByteArrayOutputStream 读取 InputStream
//        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
//        byte[] buffer = new byte[1024];
//        int bytesRead;
//
//        try {
//            while ((bytesRead = inputStream.read(buffer)) != -1) {
//                byteArrayOutputStream.write(buffer, 0, bytesRead);
//            }
//        } finally {
//            inputStream.close(); // 确保关闭输入流
//        }
//
//        // 转换为字节数组
//        byte[] channelConfigBytes = byteArrayOutputStream.toByteArray();
//
//        // 创建 ChannelConfiguration 对象
//        ChannelConfiguration channelConfiguration = new ChannelConfiguration(channelConfigBytes);
//        // 获取 Channel 配置的字节数组
//        byte[] channelConfigurationAsBytes = channelConfiguration.getChannelConfigurationAsBytes();
//
//        // 这里不要将字节数组转换为字符串，因为它可能包含二进制数据
//        // 直接将其打印为十六进制或其他形式进行调试
//        System.out.println("Channel configuration bytes: " + bytesToHex(channelConfigurationAsBytes));
//        Orderer orderer = hfClient.newOrderer("orderer1.henriport.com", "grpcs://62.234.46.212:7004");
//        if (orderer == null) {
//            throw new Exception("无法连接到 Orderer");
//        }
//        Peer peer1 = hfClient.newPeer("p1.bc1.henriport.com", "grpcs://62.234.46.212:7002");
//        Peer peer2 = hfClient.newPeer("p1.bc2.henriport.com", "grpcs://62.234.46.212:7003");
//
//        channel = hfClient.newChannel(channelName, orderer, channelConfiguration);
//        channel.addOrderer(orderer);
//        channel.addPeer(peer1);
//        channel.addPeer(peer2);
//        channel.initialize();
//    }
//
//    public void joinChannel(String channelName, Peer peer) throws Exception {
//        // Join the channel
//        channel = hfClient.getChannel(channelName);
//        channel.joinPeer(peer);
//    }
//
//    public void invokeTransaction(String chaincodeName, String fcn, String[] args) throws Exception {
//        // Create and submit a transaction proposal
//        TransactionProposalRequest request = hfClient.newTransactionProposalRequest();
//        request.setChaincodeName(chaincodeName);
//        request.setFcn(fcn);
//        request.setArgs(args);
//
//        Collection<ProposalResponse> responses = channel.sendTransactionProposal(request);
//        channel.sendTransaction(responses);
//    }
//
//    // Additional methods to handle query and transaction logic as needed
//}
